package com.kongjs.application.controller.rest;

import com.kongjs.application.annotation.OperationLogging;
import com.kongjs.application.convert.UsersConverter;
import com.kongjs.application.enums.OperationName;
import com.kongjs.application.enums.OperationType;
import com.kongjs.application.model.dto.*;
import com.kongjs.application.model.vo.*;
import com.kongjs.application.service.IUsersService;
import com.kongjs.application.util.PageUtil;
import com.kongjs.common.rest.annotation.Rest;
import jakarta.annotation.Resource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;


@Rest
@RestController
@RequestMapping("/users")
public class UsersController {
    @Resource
    private UsersConverter usersConverter;
    @Resource
    private IUsersService usersService;

    @PreAuthorize("hasAuthority('api:user:save:one')")
    @PostMapping
    @OperationLogging(operationName = OperationName.USER, operationType = OperationType.SAVE_ONE)
    public boolean save(@RequestBody @Validated SaveUserVO vo) {
        SaveUserDTO dto = usersConverter.toDto(vo);
        return usersService.save(dto);
    }

    @PreAuthorize("hasAuthority('api:user:remove:one')")
    @DeleteMapping("/{id}")
    @OperationLogging(operationName = OperationName.USER, operationType = OperationType.REMOVE_ONE)
    public boolean remove(@PathVariable("id") Long id) {
        return usersService.remove(id);
    }

    @PreAuthorize("hasAuthority('api:user:update:one')")
    @PutMapping("/{id}")
    @OperationLogging(operationName = OperationName.USER, operationType = OperationType.UPDATE_ONE)
    public boolean update(@PathVariable("id") Long id, @RequestBody @Validated UpdateUserVO vo) {
        UpdateUserDTO dto = usersConverter.toDto(vo);
        dto.setId(id);
        return usersService.update(dto);
    }

    @GetMapping("/{id}")
    @OperationLogging(operationName = OperationName.USER, operationType = OperationType.QUERY_ONE, recordResponse = false)
    public QueryUserVO query(@PathVariable("id") Long id) {
        return null;
    }

    @PreAuthorize("hasAuthority('api:user:query:page')")
    @GetMapping("/page")
    @OperationLogging(operationName = OperationName.USER, operationType = OperationType.QUERY_PAGE, recordResponse = false)
    public PageVO<QueryUserVO> page(@Validated PageQueryVO vo, QueryUserParamVO paramVO) {
        PageQueryDTO dto = new PageQueryDTO();
        dto.setPageNum(vo.getPageNum());
        dto.setPageSize(vo.getPageSize());
        QueryUserParamDTO paramDTO = usersConverter.toDto(paramVO);
        PageDTO<QueryUserDTO> pageDTO = usersService.page(dto, paramDTO);
        List<QueryUserVO> vos = usersConverter.toVos(pageDTO.getItems());
        return PageUtil.pageVo(pageDTO, vos);
    }

    @GetMapping
    @OperationLogging(operationName = OperationName.USER, operationType = OperationType.QUERY_LIST, recordResponse = false)
    public List<QueryUserVO> list() {
        return new ArrayList<>();
    }
}
